package com.fable.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Type HttpUtils
 * @Desc HttpClient工具类, 由外部注入CloseableHttpClient实例来初始化HttpClient环境<br>
 * @author tangzheng
 * @date 2017年3月25日
 * @Version V1.0
 */
public class HttpUtils {

	private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
	
	/**
	 * 默认数据格式
	 */
	private static final String DEFAULT_ENCODING = "UTF-8";

	/**
	 * 客户端
	 */
	private CloseableHttpClient client=new DefaultHttpClient();


	/**
	 * post发送http请求
	 * 
	 * @param url
	 * @return
	 */
	public String doPost(String url) {
		return doPost(url, null, null, null);
	}

	/**
	 * post发送http请求，数据按照encoding进行字符格式转换
	 * 
	 * @param url
	 * @param encoding
	 * @return
	 */
	public String doPost(String url, String encoding) {
		return doPost(url, null, null, encoding);
	}

	/**
	 * post发送http请求，带参数
	 * 
	 * @param url
	 * @param params
	 * @return
	 */
	public String doPost(String url, Map<String, String> params) {
		return doPost(url, params, null, null);
	}

	/**
	 * post发送http请求，带参数和字符格式
	 * 
	 * @param url
	 * @param params
	 * @param encoding
	 * @return
	 */
	public String doPost(String url, Map<String, String> params, String encoding) {
		return doPost(url, params, null, encoding);
	}

	/**
	 * post发送http请求，带参数和表头定义
	 * 
	 * @param url
	 * @param params
	 * @param headers
	 * @return
	 */
	public String doPost(String url, Map<String, String> params, Map<String, String> headers) {
		return doPost(url, params, headers, null);
	}

	/**
	 * post发送http请求，带参数和表头定义，参数数据按照encoding进行字符格式转换
	 * 
	 * @param url
	 * @param params
	 * @param headers
	 * @param encoding
	 * @return
	 */
	public String doPost(String url, Map<String, String> params, Map<String, String> headers, String encoding) {
		String result = "";
		try {
			HttpPost post = new HttpPost(url);
			if (null != headers) {
				Iterator<String> it = headers.keySet().iterator();
				while (it.hasNext()) {
					String key = it.next();
					post.addHeader(key, headers.get(key));
				}
			}
			if (null == encoding || "".equals(encoding.trim())) {
				encoding = DEFAULT_ENCODING;
			}
			if (null != params && params.size() > 0) {
				List<NameValuePair> cons = new ArrayList<>();
				Iterator<String> it = params.keySet().iterator();
				while (it.hasNext()) {
					String key = it.next();
					cons.add(new BasicNameValuePair(key, params.get(key)));
				}
				post.setEntity(new UrlEncodedFormEntity(cons, encoding));
			}
			HttpClientContext context = HttpClientContext.create();
			CloseableHttpResponse response = client.execute(post, context);
			try {
				HttpEntity entity = response.getEntity();
				if(response.getStatusLine().getStatusCode()!=200){
					logger.error("调用接口状态返回不为200");
					return result;
				}
				if (null != entity) {
					result = EntityUtils.toString(entity, encoding);
				}
			} finally {
				response.close();
			}
		} catch (Exception e) {
			logger.error("doPost 出错: {}", e.getMessage());
		}
		return result;
	}

	/**
	 * post发送http请求，带参数
	 * 
	 * @param url
	 * @param content
	 * @return
	 */
	public String doPostStr(String url, String content) {
		return doPostStr(url, content, null, null);
	}

	/**
	 * post发送http请求，带参数和字符格式
	 * 
	 * @param url
	 * @param content
	 * @param encoding
	 * @return
	 */
	public String doPostStr(String url, String content, String encoding) {
		return doPostStr(url, content, null, encoding);
	}

	/**
	 * post发送http请求，带参数和表头定义
	 * 
	 * @param url
	 * @param content
	 * @param headers
	 * @return
	 */
	public String doPostStr(String url, String content, Map<String, String> headers) {
		return doPostStr(url, content, headers, null);
	}

	/**
	 * post发送http请求，带参数和表头定义，参数数据按照encoding进行字符格式转换
	 * 
	 * @param url
	 * @param content
	 * @param headers
	 * @param encoding
	 * @return
	 */
	public String doPostStr(String url, String content, Map<String, String> headers, String encoding) {
		String result = "";
		try {
			HttpPost post = new HttpPost(url);
			if (null != headers) {
				Iterator<String> it = headers.keySet().iterator();
				while (it.hasNext()) {
					String key = it.next();
					post.addHeader(key, headers.get(key));
				}
			}
			if (null == encoding || "".equals(encoding.trim())) {
				encoding = DEFAULT_ENCODING;
			}
			if (null != content && !"".equals(content.trim())) {
				StringEntity entity = new StringEntity(content, encoding);
				post.setEntity(entity);
			}
			HttpClientContext context = HttpClientContext.create();
			CloseableHttpResponse response = client.execute(post, context);
			try {
				HttpEntity entity = response.getEntity();
				if(response.getStatusLine().getStatusCode()!=200){
					logger.error("调用接口状态返回不为200");
					return result;
				}
				if (null != entity) {
					result = EntityUtils.toString(entity, encoding);
				}
			} finally {
				response.close();
			}
		} catch (Exception e) {
			logger.error("doPost 出错: {}", e);
		}
		return result;
	}

	/**
	 * get发送http请求，带参数和表头定义，参数数据按照encoding进行字符格式转换
	 * 
	 * @param url
	 * @param headers
	 * @return
	 */
	public String doGet(String url, Map<String, String> headers) {
		String result = "";
		try {
			HttpGet getter = new HttpGet(url);
			if (null != headers) {
				Iterator<String> it = headers.keySet().iterator();
				while (it.hasNext()) {
					String key = it.next();
					getter.addHeader(key, headers.get(key));
				}
			}
			HttpClientContext context = HttpClientContext.create();
			CloseableHttpResponse response = client.execute(getter, context);
			try {
				HttpEntity entity = response.getEntity();
				if (null != entity) {
					result = EntityUtils.toString(entity);
				}
			} finally {
				response.close();
			}
		} catch (Exception e) {
			logger.error("doGet 出错: {}", e);
		}
		return result;
	}

	protected void doClose() {
		try {
			client.close();
		} catch (Exception e) {
			logger.error("doClose 出错: {}", e);
		}
	}


	public static void main(String[] args) {


	}
}